<?php
class cashierController extends RPHP_Controller
{

    // 收银台
    public function pay()
    {
        $m = $this->model;

        // 必要参数
        /*
        out_trade_no
        out_trade_type
        channel_id
        total_fee
        timestamp
        sign
         */
       
        $data = $m->createPayOrder($_GET);

        // 创建支付订单成功
        if ($data['code'] == 200) {
            // 微信公众号H5支付
            if ($data['channel']['trade_type'] == "JSAPI") {
                $this->view->pay = $m->getJSAPIPara($data['channel'], $data['data']['prepay_id'], $data['pay_order_id']);
                $this->display("weixin_jsapi");
            }
            // 微信APP支付
            elseif ($data['channel']['trade_type'] == "WXAPP") {
                // $this->view->pay = $m->getJSAPIPara($data['channel'], $data['data']['prepay_id'], $data['pay_order_id']);
                $this->view->data = $data['data'];
                $this->display("weixin_app");
            }
        }
        // 创建失败
        elseif ($data['code'] == 404) {
            if ($data['channel']['trade_type'] == "JSAPI") {

                // echo "CHECK DATA FAILED.";

                header("location: " . $this->c['backurl'][$data['channel']['trade_type']]['MEMBER']);
            } elseif ($data['channel']['trade_type'] == "WXAPP") {

                echo "CHECK DATA FAILED.";

                // header("location: " . $this->c['backurl'][$data['channel']['trade_type']]['MEMBER']);
            }
        } else {
            echo "ERROR " . $data['code'];
        }

    }

    /*微信公众号支付*/

    // 微信公众号支付同步返回
    public function paycallback()
    {

        $pay_order_id = intval($_GET['pay_order_id']);
        $trade_type   = addslashes(htmlspecialchars(trim($_GET['trade_type'])));
        if ($pay_order_id > 0 && $this->c['backurl'][$trade_type]) {
            $m         = $this->model;
            $pay_order = $m->getPayOrder($pay_order_id);
            if ($pay_order['out_trade_type'] != "" && $this->c['backurl'][$trade_type][$pay_order['out_trade_type']]) {
                header("location: " . $this->c['backurl'][$trade_type][$pay_order['out_trade_type']]);
            }
        }
    }

    // 微信公众号支付异步通知
    public function wxnotify()
    {

        $data['code'] = 900;
        $m            = $this->model;

        $xml = @file_get_contents('php://input');
        @libxml_disable_entity_loader(true);
        $xmlstring = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $pay_info  = @json_decode(@json_encode($xmlstring), true);

        if ($pay_info['result_code'] == "SUCCESS" && $pay_info['return_code'] == "SUCCESS" && $pay_info['transaction_id'] != "") {
            $data = $m->checkWeixinNotify($pay_info);
        }

        $this->view->data = $data;
        $this->display();
    }

    /*微信APP支付*/

    // 微信APP支付同步返回
    public function wxappcallback()
    {
        /*$pay_info['get']  = $_GET;
        $pay_info['post'] = $_POST;

        $fp = fopen("./tmp/wxappcallback.txt", "w");
        fwrite($fp, json_encode($pay_info));
        fclose($fp);*/

        $outtradeno = addslashes(htmlspecialchars(trim($_GET['outtradeno'])));
        $trade_type = "WXAPP";

        if ($outtradeno != "") {
            $pay_order_id = intval(substr($outtradeno, 15, strlen($outtradeno) - 15));
            if ($pay_order_id > 0) {
                $m         = $this->model;
                $pay_order = $m->getPayOrder($pay_order_id);
                if ($pay_order['out_trade_type'] != "" && $this->c['backurl'][$trade_type][$pay_order['out_trade_type']]) {
                    header("location: " . $this->c['backurl'][$trade_type][$pay_order['out_trade_type']]);
                }
            }
        }

    }

    // 微信APP支付异步通知
    public function wxappnotify()
    {
        /*$xml = @file_get_contents('php://input');
        @libxml_disable_entity_loader(true);
        $xmlstring = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $pay_info  = @json_decode(@json_encode($xmlstring), true);

        $fp = fopen("./tmp/wxappnotify.txt", "w");
        fwrite($fp, json_encode($pay_info));
        fclose($fp);*/

        $data['code'] = 900;
        $m            = $this->model;

        $xml = @file_get_contents('php://input');
        @libxml_disable_entity_loader(true);
        $xmlstring = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $pay_info  = @json_decode(@json_encode($xmlstring), true);

        if ($pay_info['result_code'] == "SUCCESS" && $pay_info['return_code'] == "SUCCESS" && $pay_info['transaction_id'] != "") {
            $pay_info['trade_type'] = "WXAPP";
            $data                   = $m->checkWeixinNotify($pay_info);
        }

        $this->view->data = $data;
        $this->display();

    }

}
